import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { DICT_TYPE } from '@vben/constants';

import { z } from '#/adapter/form';
import { getSimpleProductCategoryList } from '#/api/iot/product/category';
import { getDictOptions } from '@vben/hooks';

import { $t } from '#/locales';
import { getRangePickerDefaultProps } from '#/utils';

/** 新增/修改产品分类的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'name',
      label: $t('views.iot.product.category.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.iot.product.category.namePlaceholder'),
      },
      rules: z
        .string()
        .min(1, $t('views.iot.product.category.nameRequired'))
        .max(64, $t('views.iot.product.category.nameMaxLength')),
    },
    {
      fieldName: 'parentId',
      label: $t('views.iot.product.category.parentId'),
      component: 'ApiTreeSelect',
      componentProps: {
        api: getSimpleProductCategoryList,
        fieldNames: {
          label: 'name',
          value: 'id',
        },
        placeholder: $t('views.iot.product.category.parentIdPlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'sort',
      label: $t('views.common.sort'),
      component: 'InputNumber',
      componentProps: {
        placeholder: $t('views.common.sortPlaceholder'),
        class: 'w-full',
        min: 0,
      },
      rules: 'required',
    },
    {
      fieldName: 'status',
      label: $t('common.status'),
      component: 'RadioGroup',
      defaultValue: 1,
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: $t('common.statusPlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'description',
      label: $t('views.iot.product.category.description'),
      component: 'Textarea',
      componentProps: {
        placeholder: $t('views.iot.product.category.descriptionPlaceholder'),
        rows: 3,
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: $t('views.iot.product.category.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.iot.product.category.namePlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'createTime',
      label: $t('common.createTime'),
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
        class: 'w-full',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      type: 'seq',
      title: $t('views.common.seq'),
      width: 80,
    },
    {
      field: 'name',
      title: $t('views.iot.product.category.name'),
      minWidth: 200,
      treeNode: true,
    },
    {
      field: 'sort',
      title: $t('views.common.sort'),
      width: 100,
    },
    {
      field: 'status',
      title: $t('common.status'),
      width: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'description',
      title: $t('views.iot.product.category.description'),
      minWidth: 200,
    },
    {
      field: 'createTime',
      title: $t('common.createTime'),
      width: 180,
      formatter: 'formatDateTime',
    },
    {
      title: $t('common.action'),
      width: 160,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
